package cn.cai.service.impl;

import cn.cai.pojo.BasicInformation;
import cn.cai.service.BasicInformationService;
import cn.cai.utils.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * SpringBoot 启动执行的方法，将数据库中的数据全部取出
 */
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
@Slf4j
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Autowired
    private BasicInformationService basicInformationService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 判断
        if(Boolean.FALSE.equals(redisTemplate.hasKey("allTask"))){
            List<BasicInformation> list = basicInformationService.selectAll();
            redisCache.setCacheList("allTask", list);
            log.info("从数据库中读取数据");
        }
    }
}
